package com.applet.applet_shangcheng.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.applet.applet_shangcheng.dao.ImagesMapper;
import com.applet.applet_shangcheng.entity.Goods;
import com.applet.applet_shangcheng.service.GoodsService;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.applet.applet_shangcheng.utlis.QueryPageBean;
import com.applet.applet_shangcheng.utlis.Result;
import com.applet.applet_shangcheng.utlis.StatusCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
@CrossOrigin
@Api(value="商城管理",tags={"商城管理"})
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ImagesMapper imagesMapper;

    //查询所有有效
    @ApiOperation(value="查询所有上架商品")
    @GetMapping("/WXfindAllShelves")
    public Result findAllShelves() {
        Map<String, Object> map = goodsService.findAllShelves();
        if (map != null) {
            return new Result(true, StatusCode.OK, "上架商品查询成功", map);
        }
        return new Result(false, StatusCode.ERROR, "查询失败,商品不存在");
    }

    @ApiOperation(value="查询所有商品")
    @GetMapping("/findAll")
    public Result findAll() {
        Map<String, Object> map = goodsService.findAll();
        if (map != null) {
            return new Result(true, StatusCode.OK, "查询所有商品列表成功", map);
        }
        return new Result(false, StatusCode.ERROR, "查询商品列表失败,商品不存在");
    }

    @ApiOperation(value="_分页查询商品信息")
    @PostMapping("/findAlls")
    public PageResult findAlls(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = goodsService.findAlls(queryPageBean.getQueryString(), queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        return pageResult;
    }
    @ApiOperation(value="_根据goodsId获取商品信息")
    @GetMapping("/findById/{goodsId}")
    public Result findById(@PathVariable("goodsId") int goodsId) {
        Goods goods = goodsService.findById(goodsId);
        if (goods != null) {
            return new Result(true, StatusCode.OK, "商品查询成功", goods);
        }
        return new Result(false, StatusCode.ERROR, "查询失败,商品不存在");

    }

    ;
    @ApiOperation(value="_添加商品信息")
    @PostMapping("/insertGoods")
    public Result insertGoods(@RequestBody Goods goods) throws JsonProcessingException {
        Goods goodsDb2 = goodsService.findByGoodsName(goods.getGoodsName());
        if (goodsDb2 != null && goodsDb2.getGoodsName().equals(goods.getGoodsName())) {
            return new Result(false, StatusCode.ERROR, "添加商品失败,名称重复!");
        }
        Goods goodsDb = goodsService.findGoods(goods.getGoodsId());

        if (goodsDb != null && goods.getGoodsId() == goodsDb.getGoodsId()) {
            return new Result(false, StatusCode.ERROR, "添加商品失败,商品编号重复!");
        }
        if (goods.getImage()!=null){
            imagesMapper.InsertImageStatus(goods.getImage(),"1");
        }
        if (goods.getImagess()!=null) {
            List<String> imagess = goods.getImagess();
            for (String s : imagess) {
                imagesMapper.InsertImageStatus(s,"1");
            }
            ObjectMapper objectMapper = new ObjectMapper();
            String s = objectMapper.writeValueAsString(imagess);
            System.out.println(s);
            goods.setImages(s);
        }
        int result = goodsService.insertGoods(goods);
        if (result > 0) {
            return new Result(true, StatusCode.OK, "添加商品成功");
        }
        return new Result(false, StatusCode.ERROR, "添加商品失败,数据重复");
    }

    ;

    @ApiOperation(value="_修改商品信息")
    @PostMapping("/updateGoods")
    public Result updateGoods(@RequestBody Goods goods) {
        int result = goodsService.updateGoods(goods);
        if (result > 0) {
            return new Result(true, StatusCode.OK, "修改商品成功");
        }
        return new Result(false, StatusCode.ERROR, "修改商品失败");
    }

    @ApiOperation(value="_修改商品状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable("id") int id, @PathVariable("id") int status) {
        int result = goodsService.updateStatus(id, status);
        if (result > 0) {
            return new Result(true, StatusCode.OK, "修改商品状态成功");
        }
        return new Result(false, StatusCode.ERROR, "修改商品状态失败");
    }
    @ApiOperation(value="_删除商品")
    @GetMapping("/deleteGoods/{id}")
    public Result deleteGoods(@PathVariable("id") int id) {
        Goods goods = goodsService.findId(id);
        if (goods.getImage() != null) {
            imagesMapper.updateImageStatus(goods.getImage());
        }
        String images = goods.getImages();
        if (!StringUtils.isEmpty(images)) {
            JSONArray objects = JSON.parseArray(images);
            for (Object object : objects) {
                imagesMapper.updateImageStatus(object.toString());
            }
        }
        int result = goodsService.deleteGoods(id);
        if (result > 0) {
            return new Result(true, StatusCode.OK, "删除商品成功");
        }
        return new Result(false, StatusCode.ERROR, "删除商品失败");
    }
}
